package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.CheckRequest;
import com.ncst.backend.service.CheckRequestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查申请控制器
 */
@Api(tags = "检查申请管理")
@Slf4j
@RestController
@RequestMapping("/check-requests")
public class CheckRequestController {
    
    @Autowired
    private CheckRequestService checkRequestService;
    
    /**
     * 根据ID查询检查申请
     */
    @ApiOperation("根据ID查询检查申请")
    @GetMapping("/{id}")
    public Result<CheckRequest> getById(@ApiParam("检查申请ID") @PathVariable Integer id) {
        log.info("根据ID查询检查申请: {}", id);
        CheckRequest checkRequest = checkRequestService.getById(id);
        if (checkRequest != null) {
            return Result.success(checkRequest);
        } else {
            return Result.notFound("检查申请不存在");
        }
    }
    
    /**
     * 查询所有检查申请
     */
    @ApiOperation("查询所有检查申请")
    @GetMapping
    public Result<List<CheckRequest>> getAll() {
        log.info("查询所有检查申请");
        List<CheckRequest> list = checkRequestService.getAll();
        return Result.success(list);
    }
    
    /**
     * 根据条件查询检查申请列表
     */
    @ApiOperation("根据条件查询检查申请列表")
    @PostMapping("/search")
    public Result<List<CheckRequest>> getByCondition(@ApiParam("查询条件") @RequestBody CheckRequest checkRequest) {
        log.info("根据条件查询检查申请: {}", checkRequest);
        List<CheckRequest> list = checkRequestService.getByCondition(checkRequest);
        return Result.success(list);
    }
    
    /**
     * 分页查询检查申请
     */
    @ApiOperation("分页查询检查申请")
    @GetMapping("/page")
    public Result<List<CheckRequest>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询检查申请: pageNum={}, pageSize={}", pageNum, pageSize);
        List<CheckRequest> list = checkRequestService.getByPage(pageNum, pageSize);
        return Result.success(list);
    }
    
    /**
     * 统计检查申请总数
     */
    @ApiOperation("统计检查申请总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        log.info("统计检查申请总数");
        Long count = checkRequestService.countAll();
        return Result.success(count);
    }
    
    /**
     * 根据条件统计检查申请数量
     */
    @ApiOperation("根据条件统计检查申请数量")
    @PostMapping("/count")
    public Result<Long> countByCondition(@ApiParam("查询条件") @RequestBody CheckRequest checkRequest) {
        log.info("根据条件统计检查申请数量: {}", checkRequest);
        Long count = checkRequestService.countByCondition(checkRequest);
        return Result.success(count);
    }
    
    /**
     * 新增检查申请
     */
    @ApiOperation("新增检查申请")
    @PostMapping
    public Result<Void> save(@ApiParam("检查申请信息") @RequestBody CheckRequest checkRequest) {
        log.info("新增检查申请: {}", checkRequest);
        boolean success = checkRequestService.save(checkRequest);
        if (success) {
            return Result.success("新增成功");
        } else {
            return Result.error("新增失败");
        }
    }
    
    /**
     * 批量新增检查申请
     */
    @ApiOperation("批量新增检查申请")
    @PostMapping("/batch")
    public Result<Void> saveBatch(@ApiParam("检查申请列表") @RequestBody List<CheckRequest> list) {
        log.info("批量新增检查申请: 数量={}", list.size());
        boolean success = checkRequestService.saveBatch(list);
        if (success) {
            return Result.success("批量新增成功");
        } else {
            return Result.error("批量新增失败");
        }
    }
    
    /**
     * 根据ID更新检查申请
     */
    @ApiOperation("根据ID更新检查申请")
    @PutMapping("/{id}")
    public Result<Void> updateById(@ApiParam("检查申请ID") @PathVariable Integer id, @ApiParam("检查申请信息") @RequestBody CheckRequest checkRequest) {
        log.info("根据ID更新检查申请: id={}, checkRequest={}", id, checkRequest);
        checkRequest.setId(id);
        boolean success = checkRequestService.updateById(checkRequest);
        if (success) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }
    
    /**
     * 根据ID删除检查申请
     */
    @ApiOperation("根据ID删除检查申请")
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@ApiParam("检查申请ID") @PathVariable Integer id) {
        log.info("根据ID删除检查申请: {}", id);
        boolean success = checkRequestService.removeById(id);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
    
    /**
     * 批量删除检查申请
     */
    @ApiOperation("批量删除检查申请")
    @DeleteMapping("/batch")
    public Result<Void> removeByIds(@ApiParam("检查申请ID列表") @RequestBody List<Integer> ids) {
        log.info("批量删除检查申请: {}", ids);
        boolean success = checkRequestService.removeByIds(ids);
        if (success) {
            return Result.success("批量删除成功");
        } else {
            return Result.error("批量删除失败");
        }
    }

    /**
     * 开始检查：设置医生并将状态置为检查中
     */
    @ApiOperation("开始检查")
    @PutMapping("/{id}/start")
    public Result<Void> startCheck(@ApiParam("检查申请ID") @PathVariable Integer id,
                                   @ApiParam("医生ID") @RequestParam Integer employeeId) {
        log.info("开始检查 id={}, employeeId={}", id, employeeId);
        boolean success = checkRequestService.startCheck(id, employeeId);
        return success ? Result.success("已进入检查中") : Result.error("操作失败");
    }

    @ApiOperation("结果录入-列表(检查中)")
    @GetMapping("/input")
    public Result<List<CheckRequest>> listForInput(@RequestParam(required = false) String caseNumber,
                                                   @RequestParam(required = false) String realName) {
        return Result.success(checkRequestService.listForInput(caseNumber, realName));
    }

    @ApiOperation("结果录入-提交结果")
    @PutMapping("/{id}/result")
    public Result<Void> submitResult(@PathVariable Integer id,
                                     @RequestParam Integer employeeId,
                                     @RequestBody String result) {
        boolean success = checkRequestService.submitResult(id, employeeId, result);
        return success ? Result.success("结果已提交") : Result.error("提交失败");
    }
}
